package it.trekker.dialogs;

import it.trekker.R;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

public class PoiFriendlyNameDialog extends Dialog {
	
	// GUI Components
	private EditText poiFriendlyName_et = null;
	private Button okBt = null;
	private Button cancelBt = null;
	
	private INewPoi parent = null;
	
	public PoiFriendlyNameDialog(INewPoi parent) {
		
		super(parent.getContext());
		this.parent = parent;

		setContentView(R.layout.poi_friendlyname_dialog);
		setTitle("New POI");

		poiFriendlyName_et = (EditText) findViewById(R.id.poi_friendlyname);
		poiFriendlyName_et.requestFocus();
//		excursionName_et.setOnFocusChangeListener(new EditTextFocusChangeListener());
		
		Button okBt = (Button) findViewById(R.id.ok_bt);
		okBt.setOnClickListener(new OkButtonListener());
		
		Button cancelBt = (Button) findViewById(R.id.cancel_bt);
		cancelBt.setOnClickListener(new CancelButtonListener());
		
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

	}
	
	public String getExcursionName() {
		return poiFriendlyName_et.getText().toString();
	}
	
	public void setOkButtonListener(View.OnClickListener listener) {
		okBt.setOnClickListener(listener);
	}
	
	public void setCancelButtonListener(View.OnClickListener listener) {
		cancelBt.setOnClickListener(listener);
	}
	
	
	
	//************************************************************//
  	//********************* Listener classes *********************//
  	//************************************************************//
	
	private class OkButtonListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			parent.createNewPoi(getExcursionName());
			dismiss();
		}
		
	} 
	
	private class CancelButtonListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			dismiss();
		}
		
	} 
	
	private class EditTextFocusChangeListener implements View.OnFocusChangeListener {

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			if (hasFocus) {
	            PoiFriendlyNameDialog.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
	        }
		}
		
	}
	
	
	
	
	
	
	public interface INewPoi {
		public Context getContext();
		public boolean createNewPoi(String friendlyName);
	}
}
